package com.kaixin001.tk.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.kaixin001.tk.datatype.Animail;
import com.kaixin001.tk.datatype.AnimailProduct;
import com.kaixin001.tk.datatype.AnimalOnSale;
import com.kaixin001.tk.datatype.FriendBreedTool;
import com.kaixin001.tk.datatype.GardenItem;
import com.kaixin001.tk.datatype.RanchFood;
import com.kaixin001.tk.datatype.RanchInfo;
import com.kaixin001.tk.datatype.SeedItem;

public class RanchHelper {
	
	
	
    /** Log object for this class. */
    private static final Log LOG = LogFactory.getLog(RanchHelper.class);

	public static LinkedList<AnimalOnSale> selectAnimal(List<AnimalOnSale> animallist,int userRank,int currentCash)
	{
		LinkedList<AnimalOnSale>  suitableAnimals = new LinkedList<AnimalOnSale>();
		for(AnimalOnSale a: animallist)
		{
			if( a.getRank()>0 && a.getRank()<=userRank && a.getPrice()>0 && a.getPrice()<=currentCash)
			{
				suitableAnimals.add(a);
			}
		}
		java.util.Collections.sort(suitableAnimals);
		return suitableAnimals;
	}
	
	public static RanchInfo phaseRanchInfo(InputStream xmlcontent)
	{
		RanchInfo ranch = new RanchInfo();
		
		SAXBuilder builder = new SAXBuilder();
		
		Document doc;
		
		
		try {			
			doc = builder.build(xmlcontent);			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		
		
		
		Element foo = doc.getRootElement();
		List allChildren = foo.getChildren();
		
		for (int i = 0; i < allChildren.size(); i++) {
			
			String n = ((Element) allChildren.get(i)).getName(); 
			if(n.equalsIgnoreCase("animals"))
			{
				List animalItems = ((Element) allChildren.get(i)).getChildren();
				List<Animail> animails = ranch.getAnimails(); 
				for(Object item:animalItems)
				{
					Animail animail = new Animail();
										
					animail.setAid(((Element)item).getChildText("aid"));
					animail.setAname(((Element)item).getChildText("aname"));
					animail.setAnimalsid(((Element)item).getChildText("animalsid"));
					animail.setBnum(((Element)item).getChildText("bnum"));
					animail.setBproduct(((Element)item).getChildText("bproduct"));
					animail.setBstat(((Element)item).getChildText("bstat"));
					animail.setBtime(((Element)item).getChildText("btime"));
					animail.setBuid(((Element)item).getChildText("buid"));
					animail.setCtime(((Element)item).getChildText("ctime"));
					animail.setDaynum(((Element)item).getChildText("daynum"));
					animail.setFtime(((Element)item).getChildText("ftime"));
					animail.setGrow(((Element)item).getChildText("grow"));
					animail.setGtime(((Element)item).getChildText("gtime"));
					animail.setLeftptime(((Element)item).getChildText("leftptime"));
					animail.setPaction(((Element)item).getChildText("paction"));
					animail.setPnum(((Element)item).getChildText("pnum"));
					animail.setPtime(((Element)item).getChildText("ptime"));
					animail.setPtype(((Element)item).getChildText("ptype"));
					animail.setSkey(((Element)item).getChildText("skey"));
					animail.setStatus(((Element)item).getChildText("status"));
					String tips = ((Element)item).getChildText("tips");					
					animail.setTips(HttpClientHelper.Html2Text(tips.replaceAll("<br>", " ")));					
					animail.setTname(((Element)item).getChildText("tname"));
					animail.setUid(((Element)item).getChildText("uid"));
											
					animails.add(animail);
				}
				
			}
			else if(n.equalsIgnoreCase("product2"))
			{
				List productItems = ((Element) allChildren.get(i)).getChildren();
				List<AnimailProduct> products = ranch.getProducts(); 
				for(Object item:productItems)
				{
					
					AnimailProduct p = new AnimailProduct();
					
					p.setAid(((Element)item).getChildText("aid"));
					p.setMtime(((Element)item).getChildText("mtime"));
					p.setNum(((Element)item).getChildText("num"));
					p.setPname(((Element)item).getChildText("pname"));
					p.setSkey(((Element)item).getChildText("skey"));
					p.setStealnum(((Element)item).getChildText("stealnum"));
					String tips = ((Element)item).getChildText("tips");					
					p.setTips(HttpClientHelper.Html2Text(tips.replaceAll("<br>", " ")));	
					p.setTname(((Element)item).getChildText("tname"));
					p.setType(((Element)item).getChildText("type"));
					p.setUid(((Element)item).getChildText("uid"));
					
					
					products.add(p);
				}
		 }			
		 else if(n.equalsIgnoreCase("water"))			   
		 {
			 ranch.setWater(((Element)allChildren.get(i)).getText());			 
		 }
		 else if(n.equalsIgnoreCase("watertips"))			   
		 {
			 ranch.setWaterTips(HttpClientHelper.Html2Text(((Element)allChildren.get(i)).getText()));			 
		 }
		 else if(n.equalsIgnoreCase("grass"))			   
		 {
			 ranch.setGrass(((Element)allChildren.get(i)).getText());			 
		 }
		 else if(n.equalsIgnoreCase("grasstips"))			   
		 {
			 
			 ranch.setGrassTips(HttpClientHelper.Html2Text(((Element)allChildren.get(i)).getText()));			 
		 }			
		}
		
		
		return ranch;
	}
	
	
	public static List<FriendBreedTool>  processFriendtoolsList(InputStream ins)
	{

		SAXBuilder builder = new SAXBuilder();

		Document doc;

		try {
			doc = builder.build(ins);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		List<FriendBreedTool> friendTools = new LinkedList<FriendBreedTool>();
		
		Element foo = doc.getRootElement();
		List items  = foo.getChildren();

		for (Object item : items) {
			Element	animal = (Element)item;					
			FriendBreedTool tool = new FriendBreedTool();
			tool.setFuid(((Element)item).getChildText("fuid"));
			tool.setNum(((Element)item).getChildText("num"));
			tool.setRealname(((Element)item).getChildText("real_name"));
			tool.setTname(((Element)item).getChildText("tname"));
			
			friendTools.add(tool);					
		}
	

		return friendTools;
	
	}
	
	
	
	public static List<Animail>  processBreedableList(InputStream ins)
	{
		SAXBuilder builder = new SAXBuilder();

		Document doc;

		try {
			doc = builder.build(ins);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		List<Animail> animails = new LinkedList<Animail>();
		
		Element foo = doc.getRootElement();
		List allChildren = foo.getChildren();

		for (int i = 0; i < allChildren.size(); i++) {

			String n = ((Element) allChildren.get(i)).getName();
			if (n.equalsIgnoreCase("breed")) {
				List items = ((Element) allChildren.get(i)).getChildren();		

				for (Object item : items) {
					Element	animal = (Element)item;					
					Animail animail = new Animail();
					animail.setAnimalsid(((Element)item).getChildText("animalsid"));
					animail.setSkey(((Element)item).getChildText("bskey"));										
					animails.add(animail);					
				}
			}
		}

		return animails;
	}
	
	public static List<RanchFood>  processRanchFoodList(InputStream ins)
	{
	

		List<RanchFood> foods = new LinkedList<RanchFood>();
				
		SAXBuilder builder = new SAXBuilder();
		Document doc;
		try {			
			doc = builder.build(ins);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		Element foo = doc.getRootElement();
		List allChildren = foo.getChildren();
		for (int i = 0; i < allChildren.size(); i++) {
			
			String n = ((Element) allChildren.get(i)).getName(); 
			if(n.equalsIgnoreCase("food"))
			{
				List children = ((Element) allChildren.get(i)).getChildren();				
				for(Object item:children)
				{
					RanchFood seedItem = new RanchFood();
					seedItem.setSeedid(((Element)item).getChildText("seedid"));						
					seedItem.setName(((Element)item).getChildText("name"));	
					seedItem.setNum(Integer.parseInt(((Element)item).getChildText("num")));						
					foods.add(seedItem);
				}			
			}
		}
		return foods;
			
	}
}
